/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.store.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.store.entity.StoreArea;
import com.thinkgem.jeesite.modules.store.service.StoreAreaService;

/**
 * 生成store地址后台Controller
 * @author whh
 * @version 2017-09-06
 */
@Controller
@RequestMapping(value = "${adminPath}/store/storeArea")
public class StoreAreaController extends BaseController {

	@Autowired
	private StoreAreaService storeAreaService;
	
	@ModelAttribute
	public StoreArea get(@RequestParam(required=false) String id) {
		StoreArea entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = storeAreaService.get(id);
		}
		if (entity == null){
			entity = new StoreArea();
		}
		return entity;
	}
	

	@RequiresPermissions("store:storeArea:view")
	@RequestMapping(value = "form")
	public String form(StoreArea storeArea, Model model) {
		model.addAttribute("storeArea", storeArea);
		return "modules/store/storeAreaForm";
	}

	@RequiresPermissions("store:storeArea:edit")
	@RequestMapping(value = "save")
	public String save(StoreArea storeArea, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, storeArea)){
			return form(storeArea, model);
		}
		storeAreaService.save(storeArea);
		addMessage(redirectAttributes, "保存生成store地址后台成功");
		return "redirect:"+Global.getAdminPath()+"/store/storeArea/?repage";
	}
	
	@RequiresPermissions("store:storeArea:edit")
	@RequestMapping(value = "delete")
	public String delete(StoreArea storeArea, RedirectAttributes redirectAttributes) {
		storeAreaService.delete(storeArea);
		addMessage(redirectAttributes, "删除生成store地址后台成功");
		return "redirect:"+Global.getAdminPath()+"/store/storeArea/?repage";
	}

}